package com.cn.medicalcare.controller.backstageservlet.superadmin;

import com.cn.medicalcare.customtool.JsonTool;
import com.cn.medicalcare.pojo.Admin;
import com.cn.medicalcare.service.AdminService;
import com.cn.medicalcare.service.serviceimpl.AdminServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.LinkedHashMap;
import java.util.Map;

@WebServlet("/backstage/superadmin/checkusername.json")
public class CheckAdminUsernameServlet extends HttpServlet {

    private AdminService adminService = AdminServiceImpl.getInstance();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String username = req.getParameter("username");

        PrintWriter pw = resp.getWriter();
        Map<String, Object> map = new LinkedHashMap<>();
        if ((username == null || username.length() == 0)) {
            //参数错误
            map.put("status", 400);
        } else {
            Admin admin = adminService.selectAdminByUsername(username);
            if (admin == null) {
                //用户名可用
                map.put("status", 200);
            } else {
                //用户名不可用
                map.put("status", 500);
            }
        }
        pw.print(JsonTool.toJson(map));
    }
}
